package com.example.login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;

import com.example.login.fragment.CarFragment;
import com.example.login.fragment.HomeFragment;
import com.example.login.fragment.MineFragment;
import com.example.login.fragment.OrderFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private HomeFragment mHomeFragment;
    private CarFragment mCarFragment;
    private OrderFragment mOrderFragment;
    private MineFragment mMineFragment;
    private BottomNavigationView mBottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBottomNavigationView = findViewById(R.id.bottomNavigationView);
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
                    selectedFragment(0);

                } else if (item.getItemId() == R.id.car) {
                    selectedFragment(1);

                } else if (item.getItemId() == R.id.order) {
                    selectedFragment(2);

                } else {
                    selectedFragment(3);

                }
                return true;
            }
        });
        selectedFragment(0);

    }
    private void selectedFragment(int position) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        if (position == 0) {
            if (mHomeFragment==null){
                mHomeFragment=new HomeFragment();
                fragmentTransaction.add(R.id.content,mHomeFragment);
            }else {
                fragmentTransaction.show(mHomeFragment);
            }

            } else if (position==1) {
            if (mCarFragment == null) {
                mCarFragment=new CarFragment();
                fragmentTransaction.add(R.id.content,mCarFragment);
            }else {
                fragmentTransaction.show(mCarFragment);
                mCarFragment.loadData();

        }
    } else if (position==2) {
            if (mOrderFragment==null){
                mOrderFragment=new OrderFragment();
                fragmentTransaction.add(R.id.content,mOrderFragment);
            }else {
                fragmentTransaction.show(mOrderFragment);
                mOrderFragment.loadData();
            }

        }else {
            if (mMineFragment==null){
                mMineFragment=new MineFragment();
                fragmentTransaction.add(R.id.content,mMineFragment);
            }else {
                fragmentTransaction.show(mMineFragment);
            }

        }
        fragmentTransaction.commit();


    }
    private void hideFragment(FragmentTransaction fragmentTransaction ){
        if (mHomeFragment!=null) {
            fragmentTransaction.hide(mHomeFragment);
        }if (mCarFragment!=null){
            fragmentTransaction.hide(mHomeFragment);
        }if (mOrderFragment!=null){
            fragmentTransaction.hide(mHomeFragment);
        }if (mMineFragment!=null){
            fragmentTransaction.hide(mHomeFragment);
        }

    }

    }
